package com.jg.blog.utils;

import com.jg.blog.constant.RedisKeyConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Shiro相关工具类
 *
 * @Author: 杨德石
 * @Date: 2019/5/9 0009 下午 8:43
 * @Version 1.0
 */
@Component
@SuppressWarnings("all")
public class ShiroUtils {

    private static RedisTemplate redisTemplate;

    private ShiroUtils() {
    }

    /**
     * 获取登录中的用户
     *
     * @return
     */
    public static Object getLoginUser() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        return redisTemplate.boundValueOps(RedisKeyConstant.TOKEN_KEY + ":" + request.getHeader(RedisKeyConstant.TOKEN_KEY)).get();
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        ShiroUtils.redisTemplate = redisTemplate;
    }
}
